package com.gimi.cloud.jimi.manage.service.sys.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.NumberUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.dao.dao.sys.SysRoleDao;
import com.gimi.cloud.jimi.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.jimi.dto.sys.SysRoleDTO;
import com.gimi.cloud.jimi.entity.sys.SysRoleEntity;
import com.gimi.cloud.jimi.dto.params.ModifySysRoleParam;
import com.gimi.cloud.jimi.dto.params.RoleListParams;
import com.gimi.cloud.jimi.dto.params.RoleSingleParams;
import com.gimi.cloud.jimi.manage.service.sys.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shazhiwei on 2017/12/15.
 */
@Service
public class RoleServiceImpl implements RoleService {
    private final SysRoleDao roleDao;

    @Autowired
    public RoleServiceImpl(SysRoleDao roleDao){
        this.roleDao = roleDao;
    }

    @Override
    public List<SysRoleDTO> getRoleList() {
        return roleDao.getRoleList();
    }

    @Override
    public PageInfo<SysRoleDTO> getRolePageList(RoleListParams params) {
        Map<String,Object> param = new HashMap<>();
        if(!StringUtil.isBlank(params.getRoName())){
            param.put("roName","%" + params.getRoName() + "%");
        }
        String sql = "SELECT * FROM sys_role WHERE is_delete = 0 AND ro_name like :roName ORDER BY add_time DESC";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParams(param);
        PageHelper.startPage(params.getCurrentPage(),params.getPageSize());
        List<SysRoleDTO> result = roleDao.selectListDTOBySql(queryParam);
        if(result == null){
            result = new ArrayList<>();
        }
        PageInfo<SysRoleDTO> info = new PageInfo<>(result);
        return info;
    }

    @Override
    public Long getCount() {
        return roleDao.getCount();
    }

    @Override
    public SysRoleDTO getRoleSingle(RoleSingleParams params) {
        return roleDao.selectById(NumberUtil.getInt(params.getRoleId()));
    }

    @Override
    public ModifySysRoleParam addRole(ModifySysRoleParam params) {
        SysRoleEntity roleEntity = new SysRoleEntity();
        BeanUtils.copyProperties(params,roleEntity);
        roleDao.insertSelective(roleEntity);
        params.setResponseCodeEnum(ResponseCodeEnum.SUCCESSED);
        return params;
    }

    @Override
    public ModifySysRoleParam editRole(ModifySysRoleParam params) {
        SysRoleEntity roleEntity = new SysRoleEntity();
        BeanUtils.copyProperties(params,roleEntity);
        roleDao.updateByPrimaryKeySelective(roleEntity);
        params.setResponseCodeEnum(ResponseCodeEnum.SUCCESSED);
        return params;
    }

}
